<?php
class CartillasController extends AppController {

	var $uses = array('Cartilla');
	var $helpers = array('Access');

	function index() {
		$this->Cartilla->recursive = 0;
		$this->set('cartillas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid cartilla', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('cartilla', $this->Cartilla->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Cartilla->create();
			if ($this->Cartilla->save($this->data)) {
				$this->Session->setFlash(__('La cartilla ha sido guardada', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('La cartilla no se ha guardado intente de nuevo por favor.', true));
			}
		}
		$grados = $this->Cartilla->Grado->find('list',array('fields'=>array('Grado.nombre')));
		$this->set(compact('grados'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cartilla', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Cartilla->save($this->data)) {
				$this->Session->setFlash(__('La cartilla ha sido oguardada', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('La cartilla no se ha guardado intente de nuevo por favor.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Cartilla->read(null, $id);
		}
		$grados = $this->Cartilla->Grado->find('list',array('fields'=>array('Grado.nombre')));
		$this->set(compact('grados'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cartilla', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Cartilla->delete($id)) {
			$this->Session->setFlash(__('Cartilla eliminada', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('La cartilla no se ha podido eliminar', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function get_cartillas(){		
			$cartillas = $this->Cartilla->find("list", array("fields"=>array("Cartilla.id", "Cartilla.nombre")));
			
			$this->view = 'Json';
			$this->set(compact('cartillas'));
			$this->set('json', 'cartillas');		
	}
}
